﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Parichay.Data.Helper;
using Parichay.Domain;

namespace Parichay.Data.Repository
{
    public class MessageRepository : Parichay.Domain.Interface.Data.IMessageRepository
    {
        Parichay.Domain.Interface.Data.IMemberRepository _memberRepository;

        public MessageRepository(Parichay.Domain.Interface.Data.IMemberRepository memberRepository)
        {
            this._memberRepository = memberRepository;
        }

        public Domain.MemberMessage GetMessageById(int id)
        {
            return (NHibernateHelper.UniqueResult<MemberMessage>(null, "Id", id));
        }

        public bool AddMessage(Domain.MemberMessage model)
        {
            throw new NotImplementedException();
        }

        public void DeleteMessage(int id)
        {
            throw new NotImplementedException();
        }

        public bool ReplyMessage(int id, int rId, Domain.MemberMessage model)
        {
            throw new NotImplementedException();
        }

        public IList<Domain.MemberMessage> GetMessagesByUserId(int userId, int pageSize, int index)
        {
            IList<Parichay.Domain.MemberMessage> lst = NHibernateHelper.ConvertToListOf<Parichay.Domain.MemberMessage>(NHibernateHelper.Find("select distinct m from MemberMessage as m where m.Sender.Id=? or m.Recipient.Id=? and (m.ParentId=0 or m.ParentId is null) order by m.Createdon desc", new object[] { userId, userId }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32 }, false));
            return (lst);
        }


        public IList<Domain.MemberMessage> GetUserMessagesByDate(int userId, DateTime startDate, DateTime endDate)
        {
            IList<Parichay.Domain.MemberMessage> lst = NHibernateHelper.ConvertToListOf<Parichay.Domain.MemberMessage>(NHibernateHelper.Find("select distinct m from MemberMessage as m where m.Sender.Id=? or m.Recipient.Id=? and (m.ParentId=0 or m.ParentId is null) and m.Createdon<=? and m.Createdon>=? order by m.Createdon desc", new object[] { userId, userId,startDate,endDate }, new NHibernate.Type.IType[] { NHibernate.NHibernateUtil.Int32, NHibernate.NHibernateUtil.Int32,NHibernate.NHibernateUtil.DateTime, NHibernate.NHibernateUtil.DateTime }, false));
            return (lst);
        }
    }
}
